/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package program;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import javax.swing.JOptionPane;
import radiostationv41.Album;
import radiostationv41.Artist;
import radiostationv41.DBManager;
//mport static radiostationv41.DBManager.em;
import radiostationv41.Item;
import radiostationv41.Playlist;
import radiostationv41.Song;
import radiostationv41.MusicGroup;

/**
 *
 * @author KiKiWi
 */
public class EditPlaylistForm extends javax.swing.JFrame {

    Playlist playlist;
    boolean modify;
    int confirm;
   
    
    
    
    
    /**
     * Creates new form EditPlaylistForm
     */
    public EditPlaylistForm() {
        initComponents();
        modify=false;
    }
    
    public EditPlaylistForm(Playlist pl) {
        initComponents();
        modify=true;
        playlist=pl;
        jTextFieldDescription.setText(pl.getPlaylistName());
        Format formatter = new SimpleDateFormat("dd-MM-yyyy");
        String creationDate = formatter.format(pl.getPlaylistCreationDate());
        jTextFieldCreationDate.setText(creationDate);
 //       songList.clear();
        
        
        for(Song s:playlist.getSongList()){
 //           songList.add(s);
        };
        /*List<Item> itemList=new ArrayList<Item>();
        Item item;
        itemList.clear();
        for(Song s:songList){
            for(Album album:albumList){
                for(Artist artist:artistList){
                    for(MusicGroup musicGroup:musicGroupList){
                         if(s.getAlbumId().equals(album.getAlbumId()) && 
                                 (album.getArtistId().equals(artist.getArtistId()) ||
                                 album.getMusicGroupId().equals(musicGroup.getMusicGroupId()))) 
                              item=new Item(s,album, artist,musicGroup);
                              itemList.add(item);
                    }
                }
             
            }
            
        }
            
     */   
     
    }
        
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextFieldDescription = new javax.swing.JTextField();
        jTextFieldCreationDate = new javax.swing.JTextField();
        jLabelDescription = new javax.swing.JLabel();
        jLabelCreationDate = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jBtnSongIns = new javax.swing.JButton();
        jBtnSongDelete = new javax.swing.JButton();
        jBtnSave = new javax.swing.JButton();
        jBtnCancel = new javax.swing.JButton();
        jLabelListOfSongs = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTextFieldCreationDate.setToolTipText("");

        jLabelDescription.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelDescription.setText("Περιγραφή");

        jLabelCreationDate.setText("Ημερομηνία Δημιουργίας (ΗΗ-ΜΜ-ΕΕΕΕ)");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("ΦΟΡΜΑ ΔΙΑΧΕΙΡΙΣΗΣ ΛΙΣΤΑΣ");

        jBtnSongIns.setText("Εισαγωγή Τραγουδιού");

        jBtnSongDelete.setText("Διαγραφή Τραγουδιού");

        jBtnSave.setText("Αποθήκευση");
        jBtnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSaveActionPerformed(evt);
            }
        });

        jBtnCancel.setText("Ακύρωση");
        jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCancelActionPerformed(evt);
            }
        });

        jLabelListOfSongs.setText("Λίστα Τραγουδιών");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jBtnSave)
                .addGap(44, 44, 44)
                .addComponent(jBtnCancel)
                .addGap(123, 123, 123))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelListOfSongs)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabelCreationDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabelDescription, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextFieldCreationDate, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(jBtnSongIns)
                        .addGap(65, 65, 65)
                        .addComponent(jBtnSongDelete)))
                .addContainerGap(56, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldDescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldCreationDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelCreationDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(51, 51, 51)
                .addComponent(jLabelListOfSongs)
                .addGap(164, 164, 164)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnSongIns)
                    .addComponent(jBtnSongDelete))
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnCancel)
                    .addComponent(jBtnSave))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSaveActionPerformed
        // TODO add your handling code here:
        if(jTextFieldDescription.getText().isEmpty()){
            JOptionPane.showMessageDialog(null,"Η περιγραφή της λίστας είναι υποχρεωτική.", "ERROR", JOptionPane.ERROR_MESSAGE);
        }else{
               if(modify==true){
               confirm = JOptionPane.showConfirmDialog(null, "Επιθυμείτε να αποθηκεύσετε τις αλλαγές;" , "",JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
               if(confirm==0){
                   playlist.setPlaylistName(jTextFieldDescription.getText());
                   SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
                   try{
                   Date creationDate=formatter.parse(jTextFieldCreationDate.getText());
                   playlist.setPlaylistCreationDate(creationDate);
                   } catch (ParseException e){
                       System.out.println(e);
                   }
                   if(DBManager.ModifyPlaylist(playlist)){
                       ListPlaylistForm.playlistList.set(ListPlaylistForm.playlistTable.getSelectedRow(),playlist);
                       JOptionPane.showMessageDialog(null, "Επιτυχής τροποποίηση της λίστας τραγουδιών","SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE);
                       dispose();
                    }else{
                       JOptionPane.showMessageDialog(null, "Αποτυχία τροποποίησης της λίστας τραγουδιών","ERROR", JOptionPane.ERROR_MESSAGE);
                   }
               }
            }else{
                   JOptionPane.showConfirmDialog(null, "Επιθυμείτε να ολοκληρώσετε την καταχώριση;", "",JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
                   if(confirm==0){
                        pl=new Playlist();
                        pl.setPlaylistName(jTextFieldDescription.getText());
                        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
                        try{
                        Date creationDate=formatter.parse(jTextFieldCreationDate.getText());
                        pl.setPlaylistCreationDate(creationDate);
                        } catch (ParseException e){
                               System.out.println(e);
                         };
                        if(DBManager.SavePlaylist(pl)){
                            ListPlaylistForm.playlistList.add(pl);
                            JOptionPane.showMessageDialog(null,"Επιτυχής καταχώριση", "SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE);
                            jTextFieldDescription.setText("");
                            jTextFieldCreationDate.setText("");  
                        }else{
                            JOptionPane.showMessageDialog(null, "Σφάλμα επικοινωνίας με τη ΒΔ!", "ERROR", JOptionPane.ERROR_MESSAGE);
                        }
                        dispose();
                  }
                }
        }
    }//GEN-LAST:event_jBtnSaveActionPerformed

    private void jBtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCancelActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jBtnCancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditPlaylistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditPlaylistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditPlaylistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditPlaylistForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditPlaylistForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnCancel;
    private javax.swing.JButton jBtnSave;
    private javax.swing.JButton jBtnSongDelete;
    private javax.swing.JButton jBtnSongIns;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelCreationDate;
    private javax.swing.JLabel jLabelDescription;
    private javax.swing.JLabel jLabelListOfSongs;
    private javax.swing.JTextField jTextFieldCreationDate;
    private javax.swing.JTextField jTextFieldDescription;
    // End of variables declaration//GEN-END:variables
    private radiostationv41.Playlist pl;
    private radiostationv41.Song s;
}
